package com.fish.passport.callback.http;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import com.fish.common.web.BaseAction;
import com.fish.passport.dao.PassDeviceDAO;
import com.fish.passport.dao.PassPortCodeDAO;
import com.fish.passport.po.PassCode;
import com.fish.passport.po.PassDevice;
import com.fish.passport.po.PassPortCode;
import com.fish.passport.processor.UsedCodeProcessor;
import com.fish.service.passport.PassCodeService;
import com.fish.utils.DateUtil;
import com.fish.vo.Passport;

/**
 * 第三方回调驴妈妈接口实现
 * @author lipengcheng
 *
 */
public class PassportPerformCallbackAction extends BaseAction{

	private static final long serialVersionUID = -3227406740860332975L;
	private static final Log log = LogFactory.getLog(PassportPerformCallbackAction.class);
	private PassCodeService passCodeService;
	private PassPortCodeDAO passPortCodeDAO;
	private UsedCodeProcessor usedCodeProcessor;
	private PassDeviceDAO passDeviceDAO;
	private String orderId;
	private String date;
	private String adultQuantity;
	private String childQuantity;
	
	@Action("/performCallback")
	public void performCallback() {
		log.info("orderId: " + orderId);
		log.info("date: " + date);
		log.info("adultQuantity: " + adultQuantity);
		log.info("childQuantity: " + childQuantity);
		String result = "0";
		if (adultQuantity == null || "".equals(adultQuantity)) {
			adultQuantity = "0";
		}
		if (childQuantity == null || "".equals(childQuantity)) {
			childQuantity = "0";
		}
		try {
			Date useDate = DateUtil.toDate(date, "yyyyMMddHHmmss");
			if (orderId != null && useDate != null) {
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("extId", orderId.trim());
				PassCode passCode = passCodeService.getPassCodeByParams(data);
				if (passCode != null) {
					List<PassPortCode> passPortCodeList = passPortCodeDAO.queryProviderByCode(passCode.getCodeId());
					PassPortCode passPortCode = passPortCodeList.get(0);
					if (passPortCode != null) {
						// 履行对象
						String targetId = passPortCode.getTargetId();
						Map<String, Object> params = new HashMap<String, Object>();
						params.put("targetId", targetId);
						List<PassDevice> passDeviceList = passDeviceDAO.searchPassDevice(params);
						Passport passport = new Passport();
						passport.setSerialno(passCode.getSerialNo());
						passport.setPortId(targetId);
						passport.setOutPortId(targetId.toString());
						if (passDeviceList != null && passDeviceList.size() > 0) {
							passport.setDeviceId(passDeviceList.get(0).getDeviceNo().toString());
						}
						passport.setChild(childQuantity);
						passport.setAdult(adultQuantity);
						passport.setUsedDate(useDate);
						// 更新履行状态
						String code = usedCodeProcessor.update(passport);
						if ("SUCCESS".equals(code)) {
							result = "1";
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("performCallback error: ", e);
		}
		log.info("result: " + result);
		ajaxResult(result);
	}
	
	public void setPassCodeService(PassCodeService passCodeService) {
		this.passCodeService = passCodeService;
	}

	public void setPassPortCodeDAO(PassPortCodeDAO passPortCodeDAO) {
		this.passPortCodeDAO = passPortCodeDAO;
	}

	public void setUsedCodeProcessor(UsedCodeProcessor usedCodeProcessor) {
		this.usedCodeProcessor = usedCodeProcessor;
	}

	public void setPassDeviceDAO(PassDeviceDAO passDeviceDAO) {
		this.passDeviceDAO = passDeviceDAO;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getAdultQuantity() {
		return adultQuantity;
	}

	public void setAdultQuantity(String adultQuantity) {
		this.adultQuantity = adultQuantity;
	}

	public String getChildQuantity() {
		return childQuantity;
	}

	public void setChildQuantity(String childQuantity) {
		this.childQuantity = childQuantity;
	}
	
}
